<template>
  <div class="app-container">
    <!-- 顶部搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号 " prop="teamId">
        <el-input
          v-model="queryParams.teamId"
          placeholder="请输入编号 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="teamname">
        <el-input
          v-model="queryParams.teamname"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="typeCode">
        <el-input
          v-model="queryParams.typeCode"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="级别" prop="levelCode">
        <el-select v-model="queryParams.levelCode" placeholder="请选择级别" clearable>
          <el-option
            v-for="dict in dict.type.type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人 " prop="responsePerson">
        <el-input
          v-model="queryParams.responsePerson"
          placeholder="请输入负责人 "
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格上部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rm:team:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rm:team:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rm:team:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rm:team:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--表格体 -->
    <el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="编号 " align="center" prop="teamId" />
      <el-table-column label="名称" align="center" prop="teamname" />
      <el-table-column label="类型" align="center" prop="typeCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rescue_team_type" :value="scope.row.typeCode"/>
        </template>
      </el-table-column>
      <el-table-column label="级别" align="center" prop="levelCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type" :value="scope.row.levelCode"/>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="邮编 " align="center" prop="postCode" />
      <el-table-column label="值班电话" align="center" prop="telephone" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="维度" align="center" prop="latitude" />
      <el-table-column label="负责人 " align="center" prop="responsePerson" />
      <el-table-column label="电话" align="center" prop="mobilephone" />
      <el-table-column label="人数" align="center" prop="personNum" />
      <el-table-column label="主要职责" align="center" prop="mainResponsibility" />
      <el-table-column label="主要装备" align="center" prop="equipments" />
      <el-table-column label="专长描述" align="center" prop="speciality" />
      <el-table-column label="预计所需准备时间" align="center" prop="preparationTime" />
      <el-table-column label="交通工具" align="center" prop="vehicle" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rm:team:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rm:team:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改救援队伍信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号 " prop="teamId">
          <el-input v-model="form.teamId" placeholder="请输入编号 " />
        </el-form-item>
        <el-form-item label="名称" prop="teamname">
          <el-input v-model="form.teamname" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="typeCode">
          <el-input v-model="form.typeCode" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="级别" prop="levelCode">
          <el-select v-model="form.levelCode" placeholder="请选择级别">
            <el-option
              v-for="dict in dict.type.type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="邮编 " prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入邮编 " />
        </el-form-item>
        <el-form-item label="值班电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入值班电话" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="维度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="负责人 " prop="responsePerson">
          <el-input v-model="form.responsePerson" placeholder="请输入负责人 " />
        </el-form-item>
        <el-form-item label="电话" prop="mobilephone">
          <el-input v-model="form.mobilephone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="人数" prop="personNum">
          <el-input v-model="form.personNum" placeholder="请输入人数" />
        </el-form-item>
        <el-form-item label="主要职责" prop="mainResponsibility">
          <el-input v-model="form.mainResponsibility" placeholder="请输入主要职责" />
        </el-form-item>
        <el-form-item label="主要装备" prop="equipments">
          <el-input v-model="form.equipments" placeholder="请输入主要装备" />
        </el-form-item>
        <el-form-item label="专长描述" prop="speciality">
          <el-input v-model="form.speciality" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预计所需准备时间" prop="preparationTime">
          <el-input v-model="form.preparationTime" placeholder="请输入预计所需准备时间" />
        </el-form-item>
        <el-form-item label="交通工具" prop="vehicle">
          <el-input v-model="form.vehicle" placeholder="请输入交通工具" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeam, getTeam, delTeam, addTeam, updateTeam } from "@/api/resourcemanagement/team";

export default {
  name: "Team",
  dicts: ['type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 救援队伍信息表格数据
      teamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamId: null,
        teamname: null,
        typeCode: null,
        levelCode: null,
        address: null,
        responsePerson: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teamId: [
          { required: true, message: "编号 不能为空", trigger: "blur" }
        ],
        teamname: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        typeCode: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        levelCode: [
          { required: true, message: "级别不能为空", trigger: "change" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        telephone: [
          { required: true, message: "值班电话不能为空", trigger: "blur" }
        ],
        responsePerson: [
          { required: true, message: "负责人 不能为空", trigger: "blur" }
        ],
        personNum: [
          { required: true, message: "人数不能为空", trigger: "blur" }
        ],
        equipments: [
          { required: true, message: "主要装备不能为空", trigger: "blur" }
        ],
        speciality: [
          { required: true, message: "专长描述不能为空", trigger: "blur" }
        ],
        preparationTime: [
          { required: true, message: "预计所需准备时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询救援队伍信息列表 */
    getList() {
      this.loading = true;
      listTeam(this.queryParams).then(response => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teamId: null,
        teamname: null,
        typeCode: null,
        levelCode: null,
        address: null,
        postCode: null,
        telephone: null,
        longitude: null,
        latitude: null,
        responsePerson: null,
        mobilephone: null,
        personNum: null,
        mainResponsibility: null,
        equipments: null,
        speciality: null,
        preparationTime: null,
        vehicle: null,
        updateTime: null,
        createTime: null,
        notes: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加救援队伍信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTeam(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改救援队伍信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除救援队伍信息编号为"' + ids + '"的数据项？').then(function() {
        return delTeam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rm/team/export', {
        ...this.queryParams
      }, `team_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
